#-*- coding: utf-8 -*-

import socks_proxy as socks
import browser_agent as ai
import urllib2, os, threading, time

def get_one_proxy_opener(p):
    #proxy=urllib2.ProxyHandler({'http':p[0]})
    cookie=urllib2.HTTPCookieProcessor()

    #opener=urllib2.build_opener(proxy, cookie)
    opener=urllib2.build_opener(cookie)
    return opener

def get_proxy_process(p):
    # (
    #['23', '124.128.224.2', '1080', 'SOCKS4', '\xc5\xb7\xd6\xde', 
    #'ProxyCN', '05-13', '20:00
    #', '1.003', 'whois'], 
    #4.812000036239624)

    p=p[0]
    if p[3]=='SOCKS5':
        proxy=ai.ProxyProcessor(p[1], int(p[2]), socks.PROXY_TYPE_SOCKS5)
    elif p[3]=='SOCKS4':
        proxy=ai.ProxyProcessor(p[1], int(p[2]), socks.PROXY_TYPE_SOCKS4)
    elif p[3]=='HTTP':
        proxy=ai.ProxyProcessor(p[1], int(p[2]), socks.PROXY_TYPE_HTTP)
    
    return proxy

def test_proxy(s, p, success_proxys, thread_list):
    print 'test : ', p
    p=p.split()
    try:
        if p[3]=='SOCKS5':
            proxy=ai.ProxyProcessor(p[1], int(p[2]), socks.PROXY_TYPE_SOCKS5)
        elif p[3]=='SOCKS4':
            proxy=ai.ProxyProcessor(p[1], int(p[2]), socks.PROXY_TYPE_SOCKS4)
        elif p[3]=='HTTP':
            proxy=ai.ProxyProcessor(p[1], int(p[2]), socks.PROXY_TYPE_HTTP)

        opener=urllib2.build_opener(proxy)
        st=time.time()
        #res=ai.fetch('http://union.rekoo.com/test_proxy.do', fields=[('a','a')], opener=opener)
        res=ai.fetch('http://kf.51.com/help/contact.php', opener=opener)
        
        et=time.time()
        #print res['data']
        
        #if res['data']=='HTTP_X_FORWARDED_FOR: ':
        if res['data'].find(u'联系方式'.encode('gbk'))!=-1:
            success_proxys.append((p, et-st))
        #return res['data']=='HTTP_X_FORWARDED_FOR: '

    except Exception,e:
        print e

    finally:
        s.release()
        thread_list.remove(threading.currentThread())
        print -1
        

def get_proxys():
    BASE_PATH=os.path.dirname(__file__).replace('\\','/')
    if BASE_PATH:
        BASE_PATH+='/'
    proxys=open(BASE_PATH+'proxy.txt').readlines()
    s=threading.Semaphore(50)

    success_proxys=[] #成功的代理
    thread_list=[]
    for p in proxys:
        s.acquire(True)
        thr=threading.Thread(target=test_proxy, args=(s, p, success_proxys, thread_list))
        thr.start()
        thread_list.append(thr)
    
    total_wait_time=0
    while 1:
        print 'test proxy : '+'#'*20, len(thread_list)

        if len(thread_list)==0:
            break

        alive=False
        for thr in thread_list:
            if thr.isAlive():
                alive=True
        if alive:
            time.sleep(2)
            total_wait_time+=2
            if total_wait_time>30:
                break
        else:
            break
    
    #success_proxys.sort((lambda a,b:cmp(a[1], b[1])))

    #print success_proxys

    return success_proxys

if __name__=='__main__':
    get_proxys()